package com.dreamcode.caloriestracker.activities;

import android.app.Activity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import com.dreamcode.caloriestracker.collectors.ProfileDAO;
import com.dreamcode.caloriestracker.collectors.ProfileDAOimpl;
import com.dreamcode.caloriestracker.collectors.SportListAdapter;
import com.dreamcode.caloriestracker.modelclass.ProfileDetails;
import com.dreamcode.caloriestracker.utils.CalorieUtils;

public class CaloriesCalculatorActivity extends Activity {

	private Spinner groupSpinner = null;
	private Button calculate = null;
	private Button back = null;
	private RadioButton radio_calories = null;
	private RadioButton radio_time = null;
	private EditText calculateCal = null;
	private TextView textView = null;
	private ProfileDAO profileDAO = null;
	private ProfileDetails profileDetails = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.caloriescalculate);
		initView();
	}

	private void initView() {
		profileDAO = ProfileDAOimpl.getInstance();
		profileDetails = profileDAO.getProfileDetails(
				CaloriesCalculatorActivity.this, 0);
		textView = (TextView) findViewById(R.id.value_check);
		calculateCal = (EditText) findViewById(R.id.calculate_cal_min);
		radio_calories = (RadioButton) findViewById(R.id.calculate_radio_calories);
		radio_calories.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				textView.setText(" min");
				Toast.makeText(CaloriesCalculatorActivity.this,
						"Please fill time in minutes in the field ",
						Toast.LENGTH_SHORT).show();
			}

		});
		radio_time = (RadioButton) findViewById(R.id.calculate_radio_time);
		radio_time.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				textView.setText(" cal");
				Toast.makeText(CaloriesCalculatorActivity.this,
						"Please fill calories in the field ",
						Toast.LENGTH_SHORT).show();
			}

		});
		groupSpinner = (Spinner) findViewById(R.id.group_selector);
		groupSpinner.setAdapter(new SportListAdapter(
				CaloriesCalculatorActivity.this, CalorieUtils.getSports()));
		calculate = (Button) findViewById(R.id.btn_calculate);
		calculate.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				calculate();
			}
		});
		back = (Button) findViewById(R.id.btn_back);
		back.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}

		});
	}

	private void calculate() {
		if (calculateCal.getText().toString() != null
				&& !calculateCal.getText().toString().equals("")) {
			if (radio_time.isChecked()) {
				AlertDialog groupDialog = new AlertDialog.Builder(
						CaloriesCalculatorActivity.this)
						.setTitle(R.string.calculation)
						.setMessage(
								"To burn "
										+ calculateCal.getText().toString()
										+ " calories you will need to spend "
										+ CalorieUtils
												.calculateTimeByCalories(
														groupSpinner
																.getSelectedView()
																.getId(),
														Double
																.parseDouble(profileDetails
																		.getWeParam()
																		.equals(
																				"kg") ? profileDetails
																		.getProfileWeight()
																		: String
																				.valueOf(Math
																						.round(Double
																								.parseDouble(profileDetails
																										.getProfileWeight()) / 2.2))),
														Integer
																.parseInt(calculateCal
																		.getText()
																		.toString()))
										+ " minutes").setPositiveButton(
								R.string.ok, null).show();

			} else if (radio_calories.isChecked()) {
				AlertDialog groupDialog = new AlertDialog.Builder(
						CaloriesCalculatorActivity.this)
						.setTitle(R.string.calculation)
						.setMessage(
								"For "
										+ calculateCal.getText().toString()
										+ " minutes you will burn "
										+ CalorieUtils
												.calculateCaloriesByTime(
														groupSpinner
																.getSelectedView()
																.getId(),
														Double
																.parseDouble(profileDetails
																		.getWeParam()
																		.equals(
																				"kg") ? profileDetails
																		.getProfileWeight()
																		: String
																				.valueOf(Math
																						.round(Double
																								.parseDouble(profileDetails
																										.getProfileWeight()) / 2.2))),
														Integer
																.parseInt(calculateCal
																		.getText()
																		.toString()))
										+ " calories").setPositiveButton(
								R.string.ok, null).show();

			} else {
				Toast.makeText(CaloriesCalculatorActivity.this,
						"Please select one of the radio buttons",
						Toast.LENGTH_SHORT).show();
			}

		} else {
			Toast.makeText(CaloriesCalculatorActivity.this,
					"Please fill the empty field", Toast.LENGTH_SHORT).show();
		}
	}
}
